﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using resharperupdate.core.controllers;
using resharperupdate.core.controllers.impl;
using resharperupdate.core.dto;
using resharperupdate.core.services.impl;
using ResharperUpdate.Properties;

namespace ResharperUpdate
{
    public partial class Form1 : Form
    {
        private IUpdaterController controller;

        private Icon star_icon = resource_icon("ResharperUpdate.Resources.star.ico");
        private Icon arrow_icon = resource_icon("ResharperUpdate.Resources.arrow.ico");
        private Icon refresh_icon = resource_icon("ResharperUpdate.Resources.refresh.ico");
        private Icon download_icon = resource_icon("ResharperUpdate.Resources.download.ico");
        private Icon bug_icon = resource_icon("ResharperUpdate.Resources.bug.ico");
        string red_gear = "ResharperUpdate.Resources.gear_red.png";
        string green_gear = "ResharperUpdate.Resources.gear_green.png";
        string blue_gear = "ResharperUpdate.Resources.gear_blue.png";

        public Form1() : this(new UpdaterController())
        {
        }

        public Form1(IUpdaterController controller)
        {
            this.controller = controller;
            this.controller.display = show_ballon_tip;
            this.controller.status = update_tray_icon;
            this.controller.previous = add_builds_to_menu;
            InitializeComponent();
            controller.check_for_build_available();
        }

        public void show_ballon_tip(string title,string description)
        {
            notifyIcon1.ShowBalloonTip(60, title, description, new ToolTipIcon());
        }

        IDictionary<ToolStripItem, IBuild> builds_in_menu = new Dictionary<ToolStripItem, IBuild>();

        public void add_builds_to_menu(IEnumerable<IBuild> builds)
        {
            previousToolStripMenuItem.DropDownItems.Clear();
            foreach(var build in builds)
            {
                var name = String.Format("{0} - {1} - {2}",build.Date,build.Number,build.Rating);
                Image img = null;
                if (build.Status == BuildStatus.Error)
                    img = ResourceImage(red_gear);
                else if (build.Status == BuildStatus.Good)
                    img = ResourceImage(green_gear);
                else
                    img = ResourceImage(blue_gear);
                previousToolStripMenuItem.DropDownItems.Add(name,img,build_click);

                builds_in_menu.Add(previousToolStripMenuItem.DropDownItems[previousToolStripMenuItem.DropDownItems.Count-1] ,build);
            }
        }

        Image ResourceImage(string location)
        {
            return Image.FromStream(Assembly.GetEntryAssembly().GetManifestResourceStream(location));
        }

        public void build_click(object sender, EventArgs e)
        {
            controller.install(builds_in_menu[(ToolStripItem)sender]);
        }


        public void update_tray_icon(InstallStatus status)
        {
            if (status == InstallStatus.AVAILABLE)
            {
                notifyIcon1.Icon = star_icon;
                if (sounds_enabled())
                    SoundHelper.PlayWavResource("saberup.wav");
            }
            else if (status == InstallStatus.DOWNLOADING)
                notifyIcon1.Icon = download_icon;
            else if (status == InstallStatus.ERROR)
                notifyIcon1.Icon = bug_icon;
            else if (status == InstallStatus.CHECKING)
                notifyIcon1.Icon = refresh_icon;
            else if(status == InstallStatus.CURRENT)
                notifyIcon1.Icon = arrow_icon;
        }

        private bool sounds_enabled()
        {
            return !ConfigurationManager.AppSettings["I HATE FUNNY SOUNDS"].ToLower().Equals("true");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void checkForUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            controller.check_for_build_available();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            controller.install_latest();
        }

        private void installToolStripMenuItem_Click(object sender, EventArgs e)
        {
            controller.install_latest();
        }

        private static Icon resource_icon(string location)
        {
            return new Icon(Assembly.GetEntryAssembly().GetManifestResourceStream(location));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            notifyIcon1.Icon = refresh_icon;
            controller.silent_check_for_update();
        }
    }
}
